<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class InsideXiehuoExport implements FromQuery, WithHeadings, WithMapping
{
    use ModelOperatorTrait;

	protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\Summary::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$summaries = $this->queryModel($this->conditions);

    	if(isset($conditions['start_time'])){
            if(isset($conditions['end_time'])){
                $summaries = $summaries->where('created_at','>=',$conditions['start_time'])->where('created_at','<=',$conditions['end_time']);
            }
        }

        $summaries->orderBy('created_at','DESC');

        return $summaries;
    }

    public function map($summaries):array{
    	return [
    		$summaries->vehicle_number,
    		$summaries->tare,
    		$summaries->net_weight,
    		$summaries->purchase_weight,
    		$summaries->discharge_weight,
    		$summaries->relative_weight,
    		$summaries->is_void_desc,
    		$summaries->empty_weight,
    		$summaries->weight_error,
    		$summaries->is_success_desc,
    		$summaries->is_return_desc,
    		$summaries->is_go_desc,
    		$summaries->exception,
    	];
    }

    public function headings():array{
    	return [
    		'车牌号',
    		'皮重',
    		'地磅净重',
    		'采购重量',
    		'卸货重量',
    		'误差重量',
    		'是否空车过磅',
    		'过磅重量',
    		'误差重量（系统）',
    		'是否成功',
    		'是否退货',
    		'是否可以出门',
    		'异常备案'
    	];
    }
}
